package com.crm.services;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.mdao.RepairforproductMapper;
import com.crm.model.mdao.RepairrecordMapper;
import com.crm.model.mpojo.Repairforproduct;
import com.crm.model.mpojo.Repairrecord;


/**
 * 维修工单配件记录表
 * @author 梁箐
 *
 */
@Service
public class RepairForProduceServices {
	
	@Autowired
	 RepairforproductMapper repairforproductMapper;
	
	/**
	 * 工单编号查询
	 * @param rid
	 * @param pid
	 * @return
	 */
	public List<Map<String, String>> selectByRIDAndPid(Integer rid,Integer pid) {
		return repairforproductMapper.selectByRIDAndPid(rid, pid);
	}
	
	/**
	 * id查询（工单和产品）
	 * @return 
	 */
	public List<Map<String, String>> selectByID(Integer rid) {
		return repairforproductMapper.selectByRID(rid);
	}
	
	
	/**
	 * 添加记录（工单id和对应的配件记录）
	 * @return 
	 */
	public void add(List<Map<String, String>> itemNews, Integer id) {
		//遍历保存对应的维修配件记录
		//删除之前保存的同一工单编号下的对应信息
		repairforproductMapper.deleteByPrimaryKey(id);
		for (Map<String, String> map : itemNews) {
			Repairforproduct repairforproduct = new Repairforproduct();
			System.out.println("添加："+map);
			repairforproduct.setrId(Integer.parseInt(String.valueOf(map.get("rId"))));
			repairforproduct.setpId(Integer.parseInt(String.valueOf(map.get("pId"))));
			repairforproduct.setpNum(Integer.parseInt(String.valueOf(map.get("count"))));
			repairforproduct.setpFree(new BigDecimal(String.valueOf(map.get("price1"))));
			repairforproduct.setpCount(new BigDecimal(String.valueOf(map.get("discount"))));
			repairforproduct.setpCost(new BigDecimal(String.valueOf(map.get("money"))));
			repairforproduct.setRemark(String.valueOf(map.get("remark")));
			repairforproductMapper.insertSelective(repairforproduct);
		}
	}
	
	/**
	 * 查询记录（根据工单编号）
	 * @return 
	 */
	public List<Map<String, String>> selectByRid(Integer rid) {
		return repairforproductMapper.selectByRID(rid);
	}

	
}
